package springmvc.restful.controller;

import springmvc.restful.pojo.User;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * description:
 * RESTFul 设计实战
 *
 * 1. @RestController = @Controller + @ResponseBody
 * 2. @RequestParam: 接收 ?param 传参，指定是否必须和默认值
 * 3. @RequestBody: 接收 JSON 数据，将 JSON 格式转为类对象
 * 4. @PathVariable: 接收 / 路径传参，默认必须传参
 * 5. @GetMapping、@PostMapping、@PutMapping、@DeleteMapping
 */

@RestController
@RequestMapping("user")
public class UserController {

    // 分页查询业务接口，多参数用param参数传参
    @GetMapping
    public List<User> page(@RequestParam(required = false,defaultValue = "1") int page,
                           @RequestParam(required = false,defaultValue = "10") int size){
        System.out.println("page = " + page + ", size = " + size);
        return null;
    }

    // 用户保存业务接口，请求体JSON传参
    @PostMapping
    public User save(@RequestBody User user){
        System.out.println("user = " + user);
        return user;
    }

    // 用户详情业务接口，单个参数使用/路径传值
    @GetMapping("{id}")
    public User detail(@PathVariable Integer id){
        System.out.println("id = " + id);
        System.out.println("用户详情业务!");
        return null;
    }

    // 用户更新业务接口，请求体JSON传参
    @PutMapping
    public User update(@RequestBody User user){
        System.out.println("user = " + user);
        System.out.println("用户更新业务!");
        return user;
    }

    // 删除用户信息接口，单个参数使用/路径传值
    @DeleteMapping("{id}")
    public User delete(@PathVariable Integer id){
        return null;
    }

    // 条件分页查询业务接口，多参数用param参数传参
    @GetMapping("search")
    public List<User> search(String keyword,
                             @RequestParam(required = false,defaultValue = "1") int page,
                             @RequestParam(required = false,defaultValue = "10") int size){
        System.out.println("page = " + page + ", size = " + size + ", keyword = " + keyword);
        System.out.println("条件分页查询业务!");
        return null;
    }
}

